name: Bot CI/CD
on:
  push:
env:
  IS_RELEASE: ${{ gitverse.ref == 'refs/heads/master' }}
  ZIP_FILE_NAME: ./build-cache.zip
  CACHE_ID: ${{ gitverse.sha }}
  BOT_DOCKER_NAME: oiot-bot
jobs:
  build:
    name: Build app
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'gradle'
      - name: Grant execute for gradle
        run: chmod +x gradlew
      - name: Get application version
        run: echo "::set-output name=APP_VERSION::$(./gradlew -q printVersionName)"
        id: app_version
      - name: Build
        run: ./gradlew build --no-daemon
      - name: Archiving artifacts
        if: ${{ env.IS_RELEASE == 'true' }}
        uses: montudor/action-zip@v1
        with:
          args: zip -qq -9 -r ${{ env.ZIP_FILE_NAME }} ./bot/build/libs/ Dockerfile
      - name: Upload cache
        if: ${{ env.IS_RELEASE == 'true' }}
        run: curl -X POST -F file=@${{ env.ZIP_FILE_NAME }} https://cache-service.containers.cloud.ru/${{ env.CACHE_ID }}
    outputs:
      app_version: ${{ steps.app_version.outputs.APP_VERSION }}

  build-and-push-docker-image:
    if: ${{ gitverse.ref == 'refs/heads/master' }}
    needs: build
    name: Build and push Docker image
    env:
      APP_VERSION: ${{ needs.build.outputs.app_version }}
    runs-on: ubuntu-latest
    steps:
      - name: Download cache
        run: curl -o ${{ env.ZIP_FILE_NAME }} https://cache-service.containers.cloud.ru/${{ env.CACHE_ID }}
      - name: Unzip artifacts
        run: unzip -qq ${{ env.ZIP_FILE_NAME }} -d ./
      - name: Install docker
        uses: papodaca/install-docker-action@main
      - name: Log into registry
        uses: docker/login-action@v1
        with:
          registry: docker-registry.cr.cloud.ru
          username: ${{ secrets.CLOUD_ID }}
          password: ${{ secrets.CLOUD_SECRET }}
      - name: Build and Push container image to cloud evolution registry
        uses: docker/build-push-action@v4
        with:
          push: true
          tags: docker-registry.cr.cloud.ru/oiot-bot:${{ env.APP_VERSION }}
          file: ./Dockerfile
          context: ./
    outputs:
      app_version: ${{ env.APP_VERSION }}

  deploy:
    if: ${{ gitverse.ref == 'refs/heads/master' }}
    needs: build-and-push-docker-image
    name: Deploy app
    runs-on: ubuntu-latest
    env:
      APP_VERSION: ${{ needs.build-and-push-docker-image.outputs.app_version }}
    steps:
      - name: Set ssh config
        run: |          
          mkdir -p /root/.ssh
          echo "${{ secrets.SSH_SERVER_KEY }}" >> /root/.ssh/id_rsa
          echo "${{ secrets.KNOWN_HOSTS }}" >> /root/.ssh/known_hosts
          chmod 400 /root/.ssh/id_rsa
      - name: Install Docker
        uses: papodaca/install-docker-action@main
      - name: Set Docker remote context
        run: |          
          docker context create remote --docker "host=ssh://${{ secrets.SSH_SERVER_USER }}@${{ secrets.SSH_SERVER_HOST }}"
          docker context use remote
      - name: Log into registry
        run: docker login docker-registry.cr.cloud.ru -u ${{ secrets.CLOUD_ID }} -p ${{ secrets.CLOUD_SECRET }}
      - name: Check Docker container exist
        id: docker-container-exist
        run: echo "::set-output name=CONTAINER_EXIST::$(docker ps -a -f name=${{ env.BOT_DOCKER_NAME }} -q)"
      - name: Check Docker container running
        if: ${{ steps.docker-container-exist.outputs.CONTAINER_EXIST }}
        id: docker-container-running
        run: echo "::set-output name=CONTAINER_RUNNING::$(docker ps -a -f status=running -q)"
      - name: Stop Docker container
        if: ${{ steps.docker-container-exist.outputs.CONTAINER_EXIST && steps.docker-container-running.outputs.CONTAINER_RUNNING }}
        run: docker stop ${{ env.BOT_DOCKER_NAME }}
      - name: Remove Docker container
        if: ${{ steps.docker-container-exist.outputs.CONTAINER_EXIST }}
        run: docker rm ${{ env.BOT_DOCKER_NAME }}
      - name: Start new version Docker container
        run: docker run -d -t -i -e BOT_TOKEN=${{ secrets.BOT_TOKEN }} -e DB_PASSWORD=${{ secrets.BOT_DB_PASSWORD }} -e BOT_SERVICE_USERS=${{ secrets.BOT_SERVICE_USERS }} -v /home/${{ secrets.SSH_SERVER_USER }}/oiot-bot/logs:/app/logs -v /home/${{ secrets.SSH_SERVER_USER }}/oiot-bot/data:/app/data -p 8082:8082 --name ${{ env.BOT_DOCKER_NAME }} docker-registry.cr.cloud.ru/${{ env.BOT_DOCKER_NAME }}:${{ env.APP_VERSION }}
      - name: Remove old version container image
        run: docker image prune -a --force --filter "until=5m"